import Ember from 'ember';

export default Ember.Component.extend({
  classNames: ['topm-ten'],
  lastPage: Ember.computed(function() {
    var meta = this.get('meta'),
      totalPage = Math.floor(meta.total / meta.size),
      rem = meta.total % meta.size;
    if (rem === 0) {
      totalPage--;
    }
    if (totalPage < 0) {
      totalPage = 0;
    }
    return totalPage;
  }),
  page1: Ember.computed('page', function(){
    return this.get('page') + 1;
  }),
  lastPage1: Ember.computed('lastPage', function(){
    return this.get('lastPage') + 1;
  }),
  size: Ember.computed('meta', function(){
    return this.get('meta').size;
  }),
  actions: {
    pageme: function(direction) {
      if (direction === 'first') {
        this.set('page', 0);
      } else if (direction === 'previous') {
        if (this.get('page') > 0) {
          this.decrementProperty('page');
        }
      } else if (direction === 'next') {
        if (this.get('page') < this.get('lastPage')) {
          this.incrementProperty('page');
        }
      } else if (direction === 'last') {
        this.set('page', this.get('lastPage'));
      }
    }
  }
});
